package com.asset.dispoal.formula.service.commom;

import com.asset.dispoal.formula.domain.db.entitiy.GbFormula;
import com.asset.dispoal.formula.domain.db.mapper.GbFormulaMapper;
import com.asset.dispoal.formula.dto.GbFormulaRequest;
import com.asset.dispoal.formula.dto.GbFormulaResponse;
import com.asset.dispoal.formula.service.converter.GbFormulaConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/7$
 */
@Component
public class QueryPageFormulaCommand implements BaseCommand<GbFormulaRequest, Result> {


    @Autowired
    GbFormulaMapper gbFormulaMapper;

    @Override
    public Result<PageVO<GbFormulaResponse>> execute(GbFormulaRequest gbFormulaRequest) {
        final GbFormula gbFormula = GbFormulaConverter.INSTANCE.reqToDo(gbFormulaRequest);
        Page<GbFormula> page = new Page<>();
        page.setRecord(gbFormula);
        final int i = gbFormulaMapper.pageCount(page);
        PageVO<GbFormulaResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(i);
        if (i > 0) {
            page.setRows(gbFormula.getRows());
            page.setStart(gbFormula.getStart());
            page.setStart((gbFormula.getStart() - 1) * gbFormula.getRows());
            final List<GbFormula> list = gbFormulaMapper.page(page);
            final List<GbFormulaResponse> gbFormulaResponses = GbFormulaConverter.INSTANCE.dosToRes(list);
            pageVO.setResult(gbFormulaResponses);
        }
        return Result.ok(pageVO);
    }
}
